package visualTwo;


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.AbstractAction;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

public class VisualTwoApplet extends JApplet{

	/**
	 * 
	 */
	private static final long serialVersionUID = -7056077766836611525L;

	public boolean isClosing = false;
	private final VisualTwoApplet THISAPPLET = this;
	
	public VisualTwoApplet(){}

	@Override
	public void start(){
		this.setSize(650, 600);
		this.setVisible(true);
		this.setBackground(Color.BLACK);
		this.getContentPane().setBackground(Color.BLACK);
		this.setLayout(new GridLayout(1, 1));
		
		
		final WheelPanel wheel = new WheelPanel();
		
		//this.repaint();
		//this.getGraphics().setColor(Color.BLACK);
		//this.getGraphics().drawRect(0, 0, 1000, 1000);
		
		JScrollPane scrolly = new JScrollPane(wheel, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
		scrolly.setViewportView(wheel);
		scrolly.setWheelScrollingEnabled(false);
		
		scrolly.getActionMap().put("unitScrollRight", new AbstractAction(){
		    /**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			@Override
		    public void actionPerformed(ActionEvent e) {
		    }});
		scrolly.getActionMap().put("unitScrollDown", new AbstractAction(){
		    /**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			@Override
		    public void actionPerformed(ActionEvent e) {
		    }});
		scrolly.getActionMap().put("unitScrollUp", new AbstractAction(){
		    /**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			@Override
		    public void actionPerformed(ActionEvent e) {
		    }});
		scrolly.getActionMap().put("unitScrollLeft", new AbstractAction(){
		    /**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			@Override
		    public void actionPerformed(ActionEvent e) {
		    }});
		
		scrolly.setPreferredSize(new Dimension(600, 550));
		this.setLayout(new BorderLayout());
		
		JPanel menuPanel = new JPanel();
		menuPanel.setLayout(new GridLayout(1, 3));
		menuPanel.add(new JLabel("Enter Group ID"));
		final JTextField idField = new JTextField();
		menuPanel.add(idField);
		ActionListener buttonListener = new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				int id = 0;
				try{
					id = Integer.parseInt(idField.getText());
				}catch(Exception ex){
					wheel.calcGroup(0);
				}
				
				wheel.calcGroup(id);
				
			}
			
			
		};
		JButton button = new JButton("Display");
		JButton exit = new JButton("Exit");
		exit.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				THISAPPLET.stop();
			}
			
		});
		button.addActionListener(buttonListener);
		menuPanel.add(button);
		menuPanel.add(exit);
		
		menuPanel.setPreferredSize(new Dimension(600, 30));
		
		add(menuPanel,    BorderLayout.NORTH);
		add(scrolly, BorderLayout.SOUTH);
		//for(int i = 1; i<200; i++){
			wheel.calcGroup(91);
			this.validate();
			
			while(true){
				wheel.inputLogic();
				try {
					Thread.sleep(1000/120);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
			//if(i<199)
				//wheel.clear();
		//}
		//System.out.println("Done");

	}
	
	public void stop(){
		isClosing=true;
		System.out.println("stopping");
		System.exit(0);
	}

}
